package com.tangkun.environment.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.tangkun.environment.R;
import com.tangkun.environment.adapter.CheckBoxAdapter;
import com.tangkun.environment.entity.gongzuo.CheckBoxBean;
import com.tangkun.environment.utils.JumpHelper;
import com.tangkun.environment.utils.Utils;
import com.tangkun.environment.view.CustomListView;

import org.simple.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 复选框更改
 */

public class CheckBoxActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.next)
    TextView next;
    @BindView(R.id.custom_list_view)
    CustomListView customListView;
    private Context mContext;

    /**
     * ID
     */
    private String ids = "";
    /**
     * 数据源
     */
    private List<CheckBoxBean> dataList = new ArrayList<CheckBoxBean>();
    /**
     * 适配器
     */
    private CheckBoxAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //赋值公共字典中的传染病列表
        ids = getIntent().getStringExtra("ids");
        dataList = (List<CheckBoxBean>) getIntent().getSerializableExtra("data");
        setContentView(R.layout.activity_common_customlistview);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mContext = this;
        title.setText("请选择");
        next.setText("确定");
        next.setVisibility(View.VISIBLE);

        adapter = new CheckBoxAdapter(CheckBoxActivity.this, dataList);
        customListView.setAdapter(adapter);
        customListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (!TextUtils.isEmpty(dataList.get(position - 1).value) && dataList.get(position - 1).value.equals("1")) {
                    dataList.get(position - 1).value = "0";
                } else {
                    dataList.get(position - 1).value = "1";
                }
                adapter.notifyDataSetChanged();
            }
        });
    }


    @OnClick({R.id.back, R.id.next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back://返回按钮
                JumpHelper.finish(mContext);
                break;
            case R.id.next://确定
                Intent intent = new Intent();
                intent.putExtra("ids", ids);
                intent.putExtra("data", (Serializable) dataList);
                setResult(RESULT_OK, intent);
                Utils.hideSoftKeyboard(CheckBoxActivity.this, back);
                JumpHelper.finish(mContext);
                break;
        }
    }
}
